/*
 * TOP SECRET Copyright 2006-2015 Transsion.com All right reserved. This software is the confidential and proprietary
 * information of Transsion.com ("Confidential Information"). You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license agreement you entered into with Transsion.com.
 */
package com.yunji.framework_template.biz.crawler.ke;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yunji.framework_template.biz.crawler.ContentImageHandler;
import com.yunji.framework_template.biz.crawler.NewsCrawler;
import com.yunji.framework_template.biz.crawler.SourceType;
import com.yunji.framework_template.common.enumeration.CountryCode;
import com.yunji.framework_template.common.enumeration.NewsType;

/**
 * ClassName:NationNewsCrawler <br/>
 * Date: 2018年12月18日 下午3:31:24 <br/>
 * 
 * @author fenglibin1982@163.com
 * @Blog http://blog.csdn.net/fenglibing
 * @version
 * @see
 */
@Service
public class TheKenyaPostNewsCrawler extends NewsCrawler {

	@Resource
	private TheStarsNewsImageHandler theStarsNewsImageHandler;

	@Override
	public boolean isOkUrl(String url) {
		// 新闻详情url格式如：https://citizentv.co.ke/news/10yr-old-girl-found-hanging-from-a-rope-in-rarieda-223708/
		// 通过判断url的path中是否包括年月来判断其是否为新闻详情页
		URI uri;
		try {
			if (url == null || url.trim().length() == 0) {
				return false;
			}
			if (url.startsWith("mailto") || url.toLowerCase().startsWith("javascript")) {
				return false;
			}
			uri = new URI(url);
			String host = uri.getHost();
			if(host==null) {
                return false;
            }
			if (host.indexOf("kenyan-post.com") < 0) {
				return false;
			}
			String path = uri.getPath();
			String[] pathArr = path.split("/");
			if (pathArr.length == 4 && url.indexOf(".html")>0) {
				return true;
			}
		} catch (URISyntaxException e) {
		}
		return false;
	}

	@Override
	public ContentImageHandler getContentImageHandler() {
		return theStarsNewsImageHandler;
	}

	@Override
	public List<SourceType> getSourceTypeList() {
		List<SourceType> sourceTypeList = new ArrayList<SourceType>();
		sourceTypeList.add(SourceType.builder().url("http://www.kenyan-post.com").newsType(NewsType.HOT).build());
		sourceTypeList.add(SourceType.builder().url("http://www.kenyan-post.com/search/label/News").newsType(NewsType.EDUCATION).build());
		sourceTypeList.add(SourceType.builder().url("http://www.kenyan-post.com/search/label/News").newsType(NewsType.VIDEO).build());
		sourceTypeList.add(SourceType.builder().url("http://www.kenyan-post.com/search/label/Politics").newsType(NewsType.IMAGE).build());
		sourceTypeList.add(SourceType.builder().url("http://www.kenyan-post.com/search/label/County%20News").newsType(NewsType.SPORT).build());
		sourceTypeList.add(SourceType.builder().url("http://www.kenyan-post.com/search/label/Gossip%20and%20Drama").newsType(NewsType.LIFESTYLE).build());
		sourceTypeList.add(SourceType.builder().url("http://www.kenyan-post.com/search/label/Featured%20Articles").newsType(NewsType.FUN).build());
		sourceTypeList.add(SourceType.builder().url("http://www.kenyan-post.com/search/label/Entertainment%20News").newsType(NewsType.HOUSE).build());
		sourceTypeList.add(SourceType.builder().url("http://www.kenyan-post.com/search/label/Entertainment%20News").newsType(NewsType.ASKANDANSWER).build());
		sourceTypeList.add(SourceType.builder().url("http://www.kenyan-post.com/search/label/Media%20News").newsType(NewsType.HEALTH).build());
		sourceTypeList.add(SourceType.builder().url("http://www.kenyan-post.com/search/label/Gossip%20and%20Drama").newsType(NewsType.MILITARY).build());
		sourceTypeList.add(SourceType.builder().url("http://www.kenyan-post.com/search/label/Editor%27s%20Choice").newsType(NewsType.CAR).build());
		sourceTypeList.add(SourceType.builder().url("http://www.kenyan-post.com/search/label/Sports").newsType(NewsType.TECH).build());
		sourceTypeList.add(SourceType.builder().url("http://www.kenyan-post.com/search/label/Love%20and%20Relationship").newsType(NewsType.FINANCE).build());
		sourceTypeList.add(SourceType.builder().url("http://www.kenyan-post.com/search/label/Jokes").newsType(NewsType.NEWS).build());
		sourceTypeList.add(SourceType.builder().url("http://www.kenyan-post.com/search/label/Forum%20and%20discussion").newsType(NewsType.BUSINESS).build());
		sourceTypeList.add(SourceType.builder().url("http://www.kenyan-post.com/search/label/Jobs%20and%20Careers").newsType(NewsType.ENTERTAINMENT).build());
		sourceTypeList.add(SourceType.builder().url("http://www.kenyan-post.com/search/label/Expert%20Opinions").newsType(NewsType.BLOG).build());
		sourceTypeList.add(SourceType.builder().url("http://www.kenyan-post.com/search/label/County%20Politics").newsType(NewsType.MAN).build());
		sourceTypeList.add(SourceType.builder().url("http://www.kenyan-post.com/search/label/Other%20Political%20News").newsType(NewsType.WOMAN).build());
		return sourceTypeList;
	}

	@Override
    public Set<String> getCountryCodeSet() {
        Set<String> countryCodeSet = new HashSet<String>();
        countryCodeSet.add(CountryCode.KE.name());
        return countryCodeSet;
    }
}
